<template>
  <div class="pet-list">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>宠物列表</span>
          <el-button type="primary" @click="handleAdd">添加宠物</el-button>
        </div>
      </template>

      <!-- 搜索区域 -->
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="宠物名称">
          <el-input v-model="searchForm.name" placeholder="请输入宠物名称" />
        </el-form-item>
        <el-form-item label="宠物类型">
          <el-select v-model="searchForm.type" placeholder="请选择类型">
            <el-option label="猫咪" value="cat" />
            <el-option label="狗狗" value="dog" />
            <el-option label="其他" value="other" />
          </el-select>
        </el-form-item>
        <el-form-item label="领养状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态">
            <el-option label="待领养" value="available" />
            <el-option label="已领养" value="adopted" />
            <el-option label="暂不可领" value="unavailable" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 宠物列表 -->
      <el-table :data="petList" border style="width: 100%">
        <el-table-column type="selection" width="55" />
        <el-table-column label="宠物照片" width="120">
          <template #default="scope">
            <el-image
              style="width: 80px; height: 80px"
              :src="scope.row.image"
              :preview-src-list="[scope.row.image]"
              fit="cover"
            />
          </template>
        </el-table-column>
        <el-table-column prop="name" label="宠物名称" width="120" />
        <el-table-column prop="type" label="类型" width="100">
          <template #default="scope">
            <el-tag>{{ scope.row.type }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="age" label="年龄" width="100" />
        <el-table-column prop="gender" label="性别" width="80" />
        <el-table-column prop="vaccine" label="疫苗" width="120">
          <template #default="scope">
            <el-tag :type="scope.row.vaccine ? 'success' : 'warning'">
              {{ scope.row.vaccine ? '已接种' : '未接种' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ scope.row.status }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="入院时间" width="180" />
        <el-table-column label="操作" width="250" fixed="right">
          <template #default="scope">
            <el-button 
              type="primary" 
              size="small" 
              @click="handleEdit(scope.row)"
            >
              编辑
            </el-button>
            <el-button 
              type="success" 
              size="small" 
              @click="handleHealth(scope.row)"
            >
              健康档案
            </el-button>
            <el-button 
              type="danger" 
              size="small" 
              @click="handleDelete(scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 添加/编辑宠物对话框 -->
    <el-dialog
      :title="dialogTitle"
      v-model="dialogVisible"
      width="600px"
    >
      <el-form :model="petForm" :rules="rules" ref="petFormRef" label-width="100px">
        <el-form-item label="宠物照片">
          <el-upload
            class="avatar-uploader"
            action="#"
            :show-file-list="false"
            :on-change="handleImageChange"
          >
            <img v-if="petForm.image" :src="petForm.image" class="avatar" />
            <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
          </el-upload>
        </el-form-item>
        <el-form-item label="宠物名称" prop="name">
          <el-input v-model="petForm.name" />
        </el-form-item>
        <el-form-item label="宠物类型" prop="type">
          <el-select v-model="petForm.type" placeholder="请选择类型">
            <el-option label="猫咪" value="cat" />
            <el-option label="狗狗" value="dog" />
            <el-option label="其他" value="other" />
          </el-select>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input-number v-model="petForm.age" :min="0" :max="20" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-radio-group v-model="petForm.gender">
            <el-radio label="公">公</el-radio>
            <el-radio label="母">母</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="疫苗接种" prop="vaccine">
          <el-switch v-model="petForm.vaccine" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="petForm.status">
            <el-option label="待领养" value="待领养" />
            <el-option label="已领养" value="已领养" />
            <el-option label="暂不可领" value="暂不可领" />
          </el-select>
        </el-form-item>
        <el-form-item label="描述">
          <el-input 
            type="textarea" 
            v-model="petForm.description" 
            rows="3"
            placeholder="请输入宠物描述"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitForm">确定</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 健康档案对话框 -->
    <el-dialog
      title="健康档案"
      v-model="healthDialogVisible"
      width="700px"
    >
      <el-descriptions :column="2" border>
        <el-descriptions-item label="宠物名称">{{ currentPet.name }}</el-descriptions-item>
        <el-descriptions-item label="类型">{{ currentPet.type }}</el-descriptions-item>
        <el-descriptions-item label="年龄">{{ currentPet.age }}岁</el-descriptions-item>
        <el-descriptions-item label="性别">{{ currentPet.gender }}</el-descriptions-item>
      </el-descriptions>

      <el-divider content-position="left">体检记录</el-divider>
      <el-timeline>
        <el-timeline-item
          v-for="(record, index) in healthRecords"
          :key="index"
          :timestamp="record.time"
          :type="record.type"
        >
          <h4>{{ record.title }}</h4>
          <p>{{ record.content }}</p>
        </el-timeline-item>
      </el-timeline>

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="healthDialogVisible = false">关闭</el-button>
          <el-button type="primary" @click="handleAddHealth">添加记录</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'

// 搜索表单
const searchForm = reactive({
  name: '',
  type: '',
  status: ''
})

// 宠物列表数据
const petList = ref([
  {
    id: 1,
    name: 'Lucky',
    type: '金毛',
    age: 2,
    gender: '公',
    vaccine: true,
    status: '待领养',
    createTime: '2024-03-18 10:00:00',
    image: 'https://placeholder.com/150',
    description: '活泼可爱的金毛，非常适合家庭饲养'
  },
  {
    id: 2,
    name: 'Mimi',
    type: '布偶猫',
    age: 1,
    gender: '母',
    vaccine: true,
    status: '已领养',
    createTime: '2024-03-17 15:00:00',
    image: 'https://placeholder.com/150',
    description: '温顺的布偶猫，很亲人'
  }
])

// 分页相关
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)

// 对话框相关
const dialogVisible = ref(false)
const dialogTitle = ref('添加宠物')
const petFormRef = ref(null)
const petForm = reactive({
  name: '',
  type: '',
  age: 0,
  gender: '公',
  vaccine: false,
  status: '待领养',
  image: '',
  description: ''
})

const rules = {
  name: [
    { required: true, message: '请输入宠物名称', trigger: 'blur' },
    { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
  ],
  type: [
    { required: true, message: '请选择宠物类型', trigger: 'change' }
  ],
  age: [
    { required: true, message: '请输入年龄', trigger: 'blur' }
  ],
  gender: [
    { required: true, message: '请选择性别', trigger: 'change' }
  ],
  status: [
    { required: true, message: '请选择状态', trigger: 'change' }
  ]
}

// 健康档案对话框
const healthDialogVisible = ref(false)
const currentPet = reactive({})
const healthRecords = ref([
  {
    time: '2024-03-18 10:00:00',
    type: 'success',
    title: '常规体检',
    content: '体重：5.2kg，体温：38.5℃，精神状态良好'
  },
  {
    time: '2024-03-10 14:30:00',
    type: 'warning',
    title: '疫苗接种',
    content: '完成狂犬病疫苗接种'
  }
])

// 方法定义
const handleSearch = () => {
  ElMessage.success('搜索功能待实现')
}

const resetSearch = () => {
  searchForm.name = ''
  searchForm.type = ''
  searchForm.status = ''
}

const getStatusType = (status) => {
  const types = {
    '待领养': 'primary',
    '已领养': 'success',
    '暂不可领': 'info'
  }
  return types[status] || 'info'
}

const handleAdd = () => {
  dialogTitle.value = '添加宠物'
  Object.assign(petForm, {
    name: '',
    type: '',
    age: 0,
    gender: '公',
    vaccine: false,
    status: '待领养',
    image: '',
    description: ''
  })
  dialogVisible.value = true
}

const handleEdit = (row) => {
  dialogTitle.value = '编辑宠物'
  Object.assign(petForm, row)
  dialogVisible.value = true
}

const handleHealth = (row) => {
  Object.assign(currentPet, row)
  healthDialogVisible.value = true
}

const handleDelete = (row) => {
  ElMessageBox.confirm('确认删除该宠物信息吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    ElMessage.success('删除成功')
  })
}

const handleImageChange = (file) => {
  petForm.image = URL.createObjectURL(file.raw)
}

const handleAddHealth = () => {
  ElMessage.success('添加健康记录功能待实现')
}

const submitForm = async () => {
  if (!petFormRef.value) return
  
  await petFormRef.value.validate((valid) => {
    if (valid) {
      ElMessage.success(dialogTitle.value === '添加宠物' ? '添加成功' : '更新成功')
      dialogVisible.value = false
    }
  })
}

const handleSizeChange = (val) => {
  pageSize.value = val
  // 重新加载数据
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  // 重新加载数据
}
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-form {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.avatar-uploader {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 178px;
  height: 178px;
}

.avatar-uploader:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
  line-height: 178px;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style> 